using System;

using Rarous.TexyNet.Parser.Elements;

namespace Rarous.TexyNet.Parser {
  public class TexyParser : ITexyParser {
    public delegate TValue Parser<TInput, TValue>(TInput input);

    private Element _root;

    public Parser<string, Modifier> Modifier = delegate(string input) {
      return new Modifier();
    };

    public Parser<string, Element> Paragraph = delegate(string input) {
      string[] parts = input.Split(new string[] {"\n\n"}, StringSplitOptions.RemoveEmptyEntries);

      Element root = new Element();
      foreach (string s in parts) {
        string text = s.Trim();
        if (string.IsNullOrEmpty(text)) {
          continue;
        }

        Element para = new Element(ElementType.Paragraph, text);
        root.AppendChildElement(para);
      }
      return root;
    };

    public Element Root {
      get {
        return _root;
      }
    }

    public void Parse(string input) {
      _root = Paragraph(input);
    }
  }
}